home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / lib / util / cstr2arg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-02-01  |  3.0 KB  |  123 lines

  1.  
  2. #include "util.h"
  3.  
  4. /*  convert string into argument list
  5.  *
  6.  *  stash a pointer to each field into the passed array.
  7.  *  any common seperators split the words.  extra white-space
  8.  *  between fields is ignored.
  9.  *
  10.  *  if the separator is '=', then the current argument position in
  11.  *  the array points to "=", the next the one is the key and the
  12.  *  value follows it.  This permits detecting variable assignment,
  13.  *  in addition to positional arguments.
  14.  *      i.e.,  key=value ->  = key value
  15.  *
  16.  *  specially-interpreted characters:
  17.  *      space, tab, double-quote, backslash, comma, equal, slash, period,
  18.  *      semi-colon, colon, carriage return, and line-feed (newline).
  19.  *      preceding a special char with a backslash removes its
  20.  *      interpretation.  a backslash not followed by a special is used
  21.  *      to preface an octal specification for one character
  22.  *
  23.  *      a string begun with double-quote has only double-quote and
  24.  *      backslash as special characters.
  25.  *
  26.  *  a field which begins with semi-colon is interpreted as marking the
  27.  *  rest of the line as a comment and it is skipped, as are blank lines
  28.  */
  29.  
  30.  
  31. /* Steve Kille
  32.  * Modified version, which splits a string given a specific
  33.  * separator
  34.  */
  35.  
  36. extern int errno;
  37.  
  38. cstr2arg (srcptr, max, argv, dlmchar)/* convert srcptr to argument list */
  39.     register char *srcptr;  /* source data */
  40.     int max;                /* maximum number of permitted fields */
  41.     char *argv[];           /* where to put the pointers */
  42.     char dlmchar;           /* Delimiting character */
  43. {
  44.     char gotquote;      /* currently parsing quoted string */
  45.     register int ind;
  46.     register char *destptr;
  47.  
  48.     if (srcptr == 0)
  49.     {
  50.     errno = EINVAL;     /* emulate system-call failure */
  51.     return (NOTOK);
  52.     }
  53.  
  54.     for (ind = 0, max -= 2;; ind++)
  55.     {
  56.     if (ind >= max)
  57.     {
  58.         errno = E2BIG;      /* emulate system-call failure */
  59.         return (NOTOK);
  60.     }
  61.  
  62.     argv [ind] = srcptr;
  63.     destptr = srcptr;
  64.  
  65. /* */
  66.  
  67.     for (gotquote = FALSE; ; )
  68.     {
  69.         if (*srcptr == dlmchar)
  70.         {
  71.         if (gotquote)
  72.         {           /* don't interpret the char */
  73.             *destptr++ = *srcptr++;
  74.             continue;
  75.         }
  76.  
  77.         srcptr++;
  78.         *destptr = '\0';
  79.         goto nextarg;
  80.         }
  81.         else
  82.         {
  83.         switch (*srcptr)
  84.         {
  85.             default:        /* just copy it                     */
  86.             *destptr++ = *srcptr++;
  87.             break;
  88.  
  89.             case '\"':      /* beginning or end of string       */
  90.             gotquote = (gotquote) ? FALSE : TRUE;
  91.             srcptr++;   /* just toggle */
  92.             break;
  93.  
  94.             case '\\':      /* quote next character             */
  95.             srcptr++;   /* just skip the back-slash         */
  96.             if (*srcptr >= '0' && *srcptr <= '7')
  97.             {
  98.                 *destptr = '\0';
  99.                 do
  100.                     *destptr = (*destptr << 3) | (*srcptr++ - '0');
  101.                 while (*srcptr >= '0' && *srcptr <= '7');
  102.                 destptr++;
  103.                 break;
  104.             }    /* otherwise DROP ON THROUGH */
  105.             else
  106.                  *destptr++ = *srcptr++;
  107.             break;
  108.  
  109.  
  110.             case '\0':
  111.             *destptr = '\0';
  112.             ind++;
  113.             argv[ind] = (char *) 0;
  114.             return (ind);
  115.         }
  116.         }
  117.     }
  118.     nextarg:
  119.     continue;
  120.     }
  121. }
  122.  
  123.